package com.cloudwise.monitoring_system.config;

import com.cloudwise.monitoring_system.util.DynamicDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Reisen
 * @description: 配置数据源类
 * @date 2021/6/1 9:16
 */
@Configuration
public class DateSourceConfig {


        @Bean(name = "db")
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource dataSource1() {
            return DataSourceBuilder.create().build();
        }

        @Bean(name = "db2")
        @ConfigurationProperties(prefix = "spring.clickhouse")
        public DataSource dataSource2() {
            return DataSourceBuilder.create().build();
        }


        /**
         * 动态数据源: 通过AOP在不同数据源之间动态切换
         *
         * @return
         */
        @Primary
        @Bean(name = "dynamicDS1")
        public DataSource dataSource() {
            DynamicDataSource dynamicDataSource = new DynamicDataSource();
            // 默认数据源
            dynamicDataSource.setDefaultTargetDataSource(dataSource1());

            // 配置多数据源
            Map<Object, Object> dsMap = new HashMap(5);
            dsMap.put("db", dataSource1());
            dsMap.put("db2", dataSource2());

            dynamicDataSource.setTargetDataSources(dsMap);
            return dynamicDataSource;
        }
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }


}
